﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XiTi
{
    public partial class MRYTForm : Form
    {
        Encoding utf8 = new UTF8Encoding(false);
        private string subject;
        string tmp;
        private bool CoreWebView2InitializationCompleted = false;

        public MRYTForm(string km)
        {
            this.subject = km;
            InitializeComponent();
            WebView2Helper.SetWebView2ExecutableFolder(webView21, "F:\\WebView2");
            webView21.EnsureCoreWebView2Async();
            
        }

        private void MRYTForm_Load(object sender, EventArgs e)
        {
            tmp = File.ReadAllText("tmp.html", utf8);
            loadDataItem();
            tabControl1.SelectedIndex = 0;
            whiteBoardControl1.Close += WhiteBoardControl1_Close;
        }

        private void WhiteBoardControl1_Close(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 0;
        }

        private void loadDataItem()
        {
            string msg;
            JObject data = ApiHelper.getList(subject, out msg);
            if (data != null)
            {
                this.BeginInvoke(new LoadedDataItem(loadedDataItem), data);
            }
            else
            {
                lb_msg.Text = msg;
            }
        }

        delegate void LoadedDataItem(JObject data);

        private void loadedDataItem(JObject data)
        {
            lb_msg.Text = "已载入保存数据";
            DataItemListResult result = data.ToObject<DataItemListResult>();
            if (result.total > 0)
            {
                cb_list.Items.Clear();
                for (int i = 0; i < result.items.Count; i++)
                {
                    cb_list.Items.Add(result.items[i]);
                }
                cb_list.SelectedIndex = 0;
                lb_msg.Text = "共发布习题：" + result.total.ToString();
            }
        }


        private void bt_放大字体_Click(object sender, EventArgs e)
        {
            if (cb_list.SelectedItem != null)
            {
                webView21.ZoomFactor += 0.1;
            }
        }

        private void bt_缩小字体_Click(object sender, EventArgs e)
        {
            if (cb_list.SelectedItem != null)
            {
                webView21.ZoomFactor -= 0.1;
            }
        }

        private void bt_放大图片_Click(object sender, EventArgs e)
        {
            if (cb_list.SelectedItem != null)
            {
                webView21.ExecuteScriptAsync("SetImageSize(0.1)");
            }
        }

        private void bt_缩小图片_Click(object sender, EventArgs e)
        {
            if (cb_list.SelectedItem != null)
            {
                webView21.ExecuteScriptAsync("SetImageSize(-0.1)");
            }
        }

        private async void bt_shotScreen_Click(object sender, EventArgs e)
        {
            //nobook
            string FilePath = "pic";
            if (!System.IO.Directory.Exists(FilePath))
            {
                System.IO.Directory.CreateDirectory(FilePath);
            }
            MemoryStream stream = new MemoryStream();
            await webView21.CoreWebView2.CapturePreviewAsync(Microsoft.Web.WebView2.Core.CoreWebView2CapturePreviewImageFormat.Png, stream);
            var bmp = Bitmap.FromStream(stream);
            string FileName = DateTime.Now.Ticks.ToString() + ".png";
            bmp.Save(System.IO.Path.Combine(FilePath, FileName));
            System.Diagnostics.Process.Start("Explorer", "/select," + System.IO.Path.Combine(FilePath, FileName));
        }

        private async void bt_jiangjie_Click(object sender, EventArgs e)
        {
            MemoryStream stream = new MemoryStream();
            await webView21.CoreWebView2.CapturePreviewAsync(Microsoft.Web.WebView2.Core.CoreWebView2CapturePreviewImageFormat.Png, stream);
            var bmp = Bitmap.FromStream(stream);
            whiteBoardControl1.setImg(bmp);
            tabControl1.SelectedIndex = 1;
        }

        private void cb_list_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_list.SelectedItem != null && CoreWebView2InitializationCompleted)
            {
                webView21.NavigateToString(generateQuestionHtml());
            }
        }


        public String generateQuestionHtml()
        {
            DataItem d = (DataItem)cb_list.SelectedItem;
            lb_msg.Text = d.title;
            string html = d.html;
            return tmp.Replace("#question#", html);
        }

        private void webView21_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
        {
            if (e.IsSuccess)
            {
                CoreWebView2InitializationCompleted = true;
                if (cb_list.SelectedItem != null)
                {
                    webView21.NavigateToString(generateQuestionHtml());
                }
            }
            else
            {
                this.Text = e.InitializationException.Message;
            }
        }
    }
}
